package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class AIDLActivity extends AppCompatActivity implements View.OnClickListener {

    private BookInterface bookController;
    private boolean connected;
    private List<Book> bookList;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(getLocalClassName(), "service connected name  " + name + ";service = " + service);
            bookController = BookInterface.Stub.asInterface(service);
            Log.e(getLocalClassName(), "service connected bookController = " + bookController);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bookController = null;
            connected = false;
        }
    };

    private ServiceConnection serviceConnection1 = new ServiceConnection() {

        private IMyAidlInterface iMyAidlInterface;

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e(getLocalClassName(), "service connected iBinder = " + iBinder);
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
            //mBound = true;

            if (iMyAidlInterface != null) {
                try {
                    iMyAidlInterface.getData("x");
                    Log.i("TAG","x");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidlactivity);

        findViewById(R.id.btn_get_book_list).setOnClickListener(this);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_get_book_size).setOnClickListener(this);
        findViewById(R.id.btn_first_book_name).setOnClickListener(this);

        /*Intent intent = new Intent(this, BookService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);*/

    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        intent.setPackage(getPackageName());
        intent.setPackage("com.example.myapplication");
        //intent.setAction("com.example.service.action");
        boolean b = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        System.out.println(b);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connected) {
            unbindService(serviceConnection);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_get_book_list:
                if (connected) {
                    try {
                        bookList = bookController.getBookList();
                        ArrayList<Object> list = new ArrayList<>();
                        int h = 0;
                        for (int i = 0; i < bookList.size(); i++) {
                            Log.i("ruxing", "name=" + bookList.get(i).getName());
                            list.add(bookList.get(i).getName());
                        }
                        String s = bookList.toString();
                        Toast.makeText(this, list.toString(), Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_get_book_size:
                if (connected) {
                    try {
                        int size = 0;
                        size = bookController.getInt();
                        Log.i("ruxing", "共有" + size + "本书");
                        Toast.makeText(this, "共有" + size + "本书", Toast.LENGTH_SHORT).show();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_first_book_name:
                if (connected) {
                    try {
                        String name = bookController.getString();
                        Log.i("ruxing", "第一本书的书名是：" + name);
                        Toast.makeText(this, "第一本书的书名是：" + name, Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_add:
                if (connected) {
                    com.example.myapplication.Book book = new com.example.myapplication.Book("新书");
                    try {
                        bookController.addBook(book);
                        Toast.makeText(this, "向服务器添加了一本新书==="+book.getName(), Toast.LENGTH_SHORT).show();
                        Log.i("ruxing", "向服务器添加了一本新书==="+book.getName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}